package electionView;
import javax.swing.*;

import electionCtrl.Election;

import java.awt.event.*;
import java.awt.*;
import java.util.concurrent.Semaphore;

public class OverviewGui extends ElectionGui
{
	public enum Button
	{
		BEGIN_VOTE,ADMIN,END_ELECTION,REACTIVATE_SYSTEM;
		
		public String toString()
		{
			String str = super.toString();
			return str.substring(0,1) + str.substring(1).toLowerCase().replaceAll("_", " ");
		}
	};
	
	private Button action;
	/**
	 * This instance field is used in order to hold the instanciated buttons.
	 **/
	private final JButton[] button;
	
	private final JButton[] toggleButton;

	/**
	 * This constructor instanciates the Admin, Election, and Candidate array objects. It also creates the two buttons.
	 * @param cand This is the array of Candidates.
	 * @param firsd This is the admin object.
	 * @param Election This is the election object.
	 **/
	public OverviewGui(Election election, Semaphore semaphore)
	{
		super(election, new JFrame("What would you like to do?"), semaphore);
		this.mainFrame.setLayout(new FlowLayout());
		this.mainFrame.setSize(500,110);
		this.button = new JButton[Button.values().length-1];
		for(Button bt:Button.values())
		{
			ActionListener l = null;
			switch(bt)
			{
				case ADMIN:
					l = new AdminButtonListener();
					break;
				case END_ELECTION:
					l = new EndElectionButtonListener();
					break;
				default:
					continue;	
			}
			this.button[bt.ordinal()] = new JButton(bt.toString());
			this.button[bt.ordinal()].addActionListener(l);
		}
		this.toggleButton = new JButton[2];
		this.toggleButton[0] = new JButton(Button.REACTIVATE_SYSTEM.toString());
		this.toggleButton[0].addActionListener(new ReArmButtonListener());
		this.toggleButton[1] = new JButton(Button.BEGIN_VOTE.toString());
		this.toggleButton[1].addActionListener(new VoteButtonListener());
		this.button[0] = this.toggleButton[0];
		this.action = null;
		for(JButton button2: this.button)
		{
			this.mainFrame.add(button2);
		}
	}
	
	private class VoteButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			action = Button.BEGIN_VOTE;
			releaseResources();
		}
		
	}
	
	private class EndElectionButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			action = Button.END_ELECTION;
			releaseResources();
		}
	}
	
	private class AdminButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e)
		{
			action = Button.ADMIN;
			releaseResources();
		}
	}
	
	private class ReArmButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			action = Button.REACTIVATE_SYSTEM;
			releaseResources();
		}	
	}
	
	public void reActivate()
	{
		for(JButton button : this.button)
		{
			this.mainFrame.remove(button);
		}
		this.button[0] = this.toggleButton[1];
		for(JButton button : this.button)
		{
			this.mainFrame.add(button);
		}
		this.mainFrame.repaint();
	}
	
	public void deActivate()
	{
		for(JButton button : this.button)
		{
			this.mainFrame.remove(button);
		}
		this.button[0] = this.toggleButton[0];
		for(JButton button : this.button)
		{
			this.mainFrame.add(button);
		}
	}

	public Button waitForAction() throws InterruptedException
	{
		this.acquireResources();
		Button b = this.action;
		this.action = null;
		return b;
	}
}
